package net.sk.china.city.talk.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.city.talk.model.system.SystemAttachment;
import net.sk.china.city.talk.request.system.SystemAttachmentMoveRequest;
import net.sk.china.city.talk.request.system.SystemAttachmentRequest;
import net.sk.china.city.talk.service.system.SystemAttachmentService;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import net.sk.china.common.utils.TalkUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/3 16:08
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("system/attachment/")
@Api(tags = "附件管理")
public class SystemAttachmentController {

    private final SystemAttachmentService systemAttachmentService;

  public SystemAttachmentController(SystemAttachmentService systemAttachmentService) {
        this.systemAttachmentService = systemAttachmentService;
    }


    @PreAuthorize("hasAuthority('admin:system:attachment:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<CommonPage<SystemAttachment>> getList(
            @RequestParam @Validated Integer pid,
            @RequestParam(value = "attType", required = false) String attType,
            @Validated PageParamRequest pageParamRequest) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("pid", pid);
        map.put("attType", attType);
        CommonPage<SystemAttachment> systemAttachmentCommonPage = CommonPage.restPage(systemAttachmentService.getList(map, pageParamRequest));
        return CommonResult.success(systemAttachmentCommonPage);
    }


    @PreAuthorize("hasAuthority('admin:system:attachment:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated SystemAttachmentRequest systemAttachmentRequest) {
        if (systemAttachmentService.add(systemAttachmentRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('admin:system:attachment:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "delete/{ids}", method = RequestMethod.GET)
    public CommonResult<String> delete(@PathVariable String ids) {
        if (systemAttachmentService.removeByIds(TalkUtil.stringToArray(ids))) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:system:attachment:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id,
                                       @RequestBody @Validated SystemAttachmentRequest systemAttachmentRequest) {
        systemAttachmentRequest.setAttId(id);
        if (systemAttachmentService.edit(systemAttachmentRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:system:attachment:move')")
    @ApiOperation(value = "更改图片目录")
    @RequestMapping(value = "move", method = RequestMethod.POST)
    public CommonResult<String> updateAttrId(@RequestBody @Validated SystemAttachmentMoveRequest move) {
        if (systemAttachmentService.updateAttrId(move)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:system:attachment:info')")
    @ApiOperation(value = "附件详情")
    @RequestMapping(value = "info/{id}", method = RequestMethod.GET)
    public CommonResult<SystemAttachment> info(@PathVariable Integer id) {
        return CommonResult.success(systemAttachmentService.getById(id));
    }
}
